package com.luych.toolbox.data.service.controller;

import com.luych.toolbox.common.constant.PageConstant;
import com.luych.toolbox.common.feign.intf.database.service.DataCompareHistoryFeign;
import com.luych.toolbox.common.feign.param.data.service.DataCompareHistoryParam;
import com.luych.toolbox.common.feign.view.data.service.DataCompareHistoryColumnView;
import com.luych.toolbox.common.feign.view.data.service.DataCompareHistoryTableView;
import com.luych.toolbox.common.feign.view.data.service.DataCompareHistoryView;
import com.luych.toolbox.common.page.CommonPageParam;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.common.page.CommonSort;
import com.luych.toolbox.common.page.PageResult;
import com.luych.toolbox.common.service.base.BaseController;
import com.luych.toolbox.data.service.entity.DataCompareHistory;
import com.luych.toolbox.data.service.service.DataCompareHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "数据对比历史")
@RestController
@RequestMapping("/dch")
public class DataCompareHistoryController
        extends BaseController<DataCompareHistoryView, DataCompareHistoryParam, DataCompareHistory>
        implements DataCompareHistoryFeign {

    private final DataCompareHistoryService dataCompareHistoryService;

    @Autowired
    public DataCompareHistoryController(DataCompareHistoryService dataCompareHistoryService) {
        super(dataCompareHistoryService);
        this.dataCompareHistoryService = dataCompareHistoryService;
    }

    @ApiOperation("按关键字查询数据对比历史信息")
    @GetMapping("/search/keyword")
    public CommonPageResult<DataCompareHistoryView> pageByKeyword(
            @ApiParam(value = "每页容量") @RequestHeader(name = "pageSize", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE_STR) long pageSize,
            @ApiParam(value = "查询页码") @RequestHeader(name = "pageNumber", required = false, defaultValue = PageConstant.DEFAULT_PAGE_NUMBER_STR) long pageNumber,
            @ApiParam(value = "排序字段") @RequestHeader(name = "sort", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SORT) String sort,
            @ApiParam(value = "正序/倒序，取值ASC/DESC") @RequestHeader(name = "order", required = false, defaultValue = PageConstant.DEFAULT_PAGE_ORDER) String order,
            @ApiParam(value = "查询关键字，支持按名称模糊查询") @RequestParam(name = "keyword", required = false) String keyword
    ) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<DataCompareHistoryView> pageResult = dataCompareHistoryService.pageByKeyword(commonPageParam, keyword);
        return CommonPageResult.getInstance(pageResult);
    }

    @ApiOperation("查询数据对比历史的数据表差异信息")
    @GetMapping("{id}/table")
    public CommonPageResult<DataCompareHistoryTableView> pageCompareTables(
            @ApiParam(value = "每页容量") @RequestHeader(name = "pageSize", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE_STR) long pageSize,
            @ApiParam(value = "查询页码") @RequestHeader(name = "pageNumber", required = false, defaultValue = PageConstant.DEFAULT_PAGE_NUMBER_STR) long pageNumber,
            @ApiParam(value = "排序字段") @RequestHeader(name = "sort", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SORT) String sort,
            @ApiParam(value = "正序/倒序，取值ASC/DESC") @RequestHeader(name = "order", required = false, defaultValue = PageConstant.DEFAULT_PAGE_ORDER) String order,
            @ApiParam(value = "数据对比历史ID") @PathVariable(name = "id") String id,
            @ApiParam(value = "是否不同") @RequestParam(name = "diff", required = false) Boolean diff,
            @ApiParam(value = "数据表名称，模糊查询") @RequestParam(name = "table", required = false) String table
    ) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<DataCompareHistoryTableView> pageResult = dataCompareHistoryService.pageCompareTables(commonPageParam, id, diff, table);
        return CommonPageResult.getInstance(pageResult);
    }

    @ApiOperation("查询数据对比历史的数据字段差异信息")
    @GetMapping("{id}/column")
    public CommonPageResult<DataCompareHistoryColumnView> pageCompareColumns(
            @ApiParam(value = "每页容量") @RequestHeader(name = "pageSize", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE_STR) long pageSize,
            @ApiParam(value = "查询页码") @RequestHeader(name = "pageNumber", required = false, defaultValue = PageConstant.DEFAULT_PAGE_NUMBER_STR) long pageNumber,
            @ApiParam(value = "排序字段") @RequestHeader(name = "sort", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SORT) String sort,
            @ApiParam(value = "正序/倒序，取值ASC/DESC") @RequestHeader(name = "order", required = false, defaultValue = PageConstant.DEFAULT_PAGE_ORDER) String order,
            @ApiParam(value = "数据对比历史ID") @PathVariable(name = "id") String id,
            @ApiParam(value = "是否不同") @RequestParam(name = "diff", required = false) Boolean diff,
            @ApiParam(value = "数据表名称") @RequestParam(name = "table") String table,
            @ApiParam(value = "数据字段名称，模糊查询") @RequestParam(name = "column", required = false) String column
    ) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<DataCompareHistoryColumnView> pageResult = dataCompareHistoryService.pageCompareColumns(commonPageParam, id, diff, table, column);
        return CommonPageResult.getInstance(pageResult);
    }

    @ApiOperation("清空数据对比历史")
    @DeleteMapping
    public void delete() {
        dataCompareHistoryService.truncate();
    }
}
